"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ThreeHelper = void 0;
/*
 * @Author: hongbin
 * @Date: 2022-12-10 08:23:15
 * @LastEditors: hongbin
 * @LastEditTime: 2023-03-18 11:21:52
 * @Description:Three.js 包装类
 */
var THREE = require("three");
var AnimationPlayer_1 = require("./utils/AnimationPlayer");
var BaseEnvironment_1 = require("./utils/BaseEnvironment");
var gui_1 = require("./helper/gui");
var stats_1 = require("./helper/stats");
var LinearAnimation_1 = require("./utils/LinearAnimation");
var utils_1 = require("./utils");
var three_1 = require("three");
var SkeletonAnimation_1 = require("./utils/SkeletonAnimation");
var ThreeHelper = /** @class */ (function (_super) {
    __extends(ThreeHelper, _super);
    function ThreeHelper(params) {
        var _this = _super.call(this, params) || this;
        _this.frameHandle = 0;
        _this.framing = false;
        _this._animation = function () { };
        _this.AnimationPlayer = AnimationPlayer_1.AnimationPlayer;
        _this.SkeletonAnimation = SkeletonAnimation_1.SkeletonAnimation;
        _this.LinearAnimation = LinearAnimation_1.LinearAnimation;
        _this.RandomColor = utils_1.RandomColor;
        _this.stats = stats_1.stats;
        _this.clock = new THREE.Clock();
        _this.runAnimate = true;
        _this.generateRect = ThreeHelper.generateRect;
        _this.createSphere = ThreeHelper.createSphere;
        if (ThreeHelper.instance)
            return ThreeHelper.instance;
        ThreeHelper.instance = _this;
        return _this;
    }
    /**
     * 添加性能指示器
     */
    ThreeHelper.prototype.addStats = function () {
        this.stats.init();
    };
    /**
     * 添加gui调试工具
     */
    ThreeHelper.prototype.addGUI = function (guiName) {
        this.gui = (0, gui_1.initGUI)(guiName);
    };
    /**
     * 向环境中添加物体
     */
    ThreeHelper.prototype.add = function () {
        var _a;
        var object = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            object[_i] = arguments[_i];
        }
        (_a = this.scene).add.apply(_a, object);
    };
    /**
     * 生成矩形 Generate Rectangle
     */
    ThreeHelper.generateRect = function (geometryParams, parameters) {
        var _a;
        var geometry = new ((_a = THREE.BoxGeometry).bind.apply(_a, __spreadArray([void 0], Object.values(geometryParams), false)))();
        // const material = new THREE.MeshPhysicalMaterial(parameters);
        var material = new THREE.MeshStandardMaterial(parameters);
        var box = new THREE.Mesh(geometry, material);
        return box;
    };
    /**
     * 创建矩形
     */
    ThreeHelper.prototype.addRect = function (geometryParams, parameters) {
        var box = ThreeHelper.generateRect(geometryParams, parameters);
        this.add(box);
        //默认物体中心在世界坐标轴上 调整到下方对齐世界坐标轴
        box.position.y += geometryParams.height / 2;
        this.expandBoxTexture(box);
        return box;
    };
    /**
     * 创建球形
     */
    ThreeHelper.createSphere = function (_a, parameters) {
        var radius = _a.radius, widthSegments = _a.widthSegments, heightSegments = _a.heightSegments;
        var geometry = new THREE.SphereGeometry(radius, widthSegments, heightSegments);
        var material = new THREE.MeshStandardMaterial(parameters);
        var mesh = new three_1.Mesh(geometry, material);
        return mesh;
    };
    /**
     *拓展
     */
    ThreeHelper.prototype.expandBoxTexture = function (box) {
        var _this = this;
        box.setBoxTexture = function () {
            var texts = [];
            for (var _i = 0; _i < arguments.length; _i++) {
                texts[_i] = arguments[_i];
            }
            var materials = texts.map(function (t) { return new THREE.MeshStandardMaterial({ map: _this.loadTexture(t) }); });
            box.material = materials;
            materials.forEach(function (m) {
                if (m.map) {
                    m.map.encoding = THREE.sRGBEncoding;
                }
            });
        };
    };
    /**
     * 向物体上增加贴图
     */
    ThreeHelper.prototype.setMaterialMap = function (mesh, map, onload) {
        if (mesh && !Array.isArray(mesh.material) && mesh.material) {
            mesh.material.map = this.loadTexture(map, function (texture) {
                texture.flipY = false;
                texture.encoding = THREE.sRGBEncoding;
                onload && onload();
            });
        }
        else {
            console.log(mesh);
        }
    };
    /**
     * @description: 向物体上增加贴图
     * @param {THREE} scene 模型组
     * @param {string} childName 要设置贴图的子集name
     * @param {string} map 贴图url
     * @param {VoidFunction} onload 纹理加载贴图完毕的回调
     * @return {*}
     */
    ThreeHelper.prototype.setMaterialMapOnChild = function (scene, childName, map, onload) {
        var bottle = scene.getObjectByName(childName);
        if (bottle)
            this.setMaterialMap(bottle, map, onload);
        else
            return new Error("\u672A\u83B7\u53D6\u5230\u6A21\u578B\u4E2D\u6709 ".concat(childName, " \u5B50\u96C6"));
    };
    /**
     * 镜头自动旋转
     */
    ThreeHelper.prototype.autoRotate = function () {
        if (this.controls) {
            this.controls.autoRotate = true;
            this.controls.enableRotate = true;
        }
    };
    /**
     * 设置每帧渲染执行的操作
     */
    ThreeHelper.prototype.animation = function (call) {
        this._animation = call;
    };
    /**
     * 逐帧渲染 frame(帧)
     */
    ThreeHelper.prototype.frameByFrame = function () {
        var _this = this;
        var _a;
        this.frameHandle = requestAnimationFrame(function () { return _this.frameByFrame(); });
        (_a = this.controls) === null || _a === void 0 ? void 0 : _a.update();
        this.runAnimate && this._animation();
        this.render();
        this.stats.update();
    };
    /**
     *  停止逐帧渲染
     */
    ThreeHelper.prototype.stopFrame = function () {
        cancelAnimationFrame(this.frameHandle);
        this.frameHandle = 0;
    };
    return ThreeHelper;
}(BaseEnvironment_1.BaseEnvironment));
exports.ThreeHelper = ThreeHelper;
